package com.patterns.server;

import java.util.Collection;

import com.patterns.Cache;
import com.patterns.connection.Connection;
import com.patterns.connection.Listener;
import com.patterns.connection.ServerConnection;

public class Server {

	private Cache masterCache;
	private Collection<Listener> listeners;
	private Connection connection;
	
	public Server(Connection connect){
		connection = connect;
		listeners = ((ServerConnection) connect).getListeners();
		masterCache = new Cache();
	}
	
	public void putInCache(String s, Object o){
		masterCache.putInCache(s, o);
		notifyListeners();
	}

	private void notifyListeners() {
		for (Listener listener : listeners) {
			listener.updateInfo(masterCache.getCopy());
		}
	}

	public Cache getMasterCache() {
		return masterCache;
	}
	
	public void shutDown(){
		connection.shutDown();
	}
	

}
